/*txBx9FIYfUQl2UukqRyP73+PxnwFNg9cG2D09QQ88Ef4PaPs4BTnj4ZfDFTpSXII*/
import initButtonState from "../init/initButtonState";
import { PAGECODE, STATUS, FIELD, URL, DataSource } from '../../constance';

export default function (pageModel) {
    const {
        dataFormat: { cardSimpleData },
        formula: { validateToSave, dealDisplayFormula: dealFormulamsg },
        cardPagination,
        multiLang: { getMultiLangData },
        toast,
        spaCache,
        ajax
    } = pageModel;
    const { updateCache, addCache } = spaCache;
    const multiLangData = getMultiLangData();
    pageModel.get(PAGECODE.cardbody1).filterEmptyRows([]);
    pageModel.get(PAGECODE.cardbody2).filterEmptyRows([]);
    let flag = pageModel.get(PAGECODE.cardHead).validate(); //表单必输校验
    if (!flag) {
        return;
    }
    flag = pageModel.get(PAGECODE.cardbody1).validate() && pageModel.get(PAGECODE.cardbody2).validate(); //表格必输项校验
    if (!flag) {
        return;
    }
    //创建保存的聚合VO createExtCarddataimple
    let simpleDataParams = {
        pageCode: PAGECODE.cardCode,
        formCode: PAGECODE.cardHead,
        tableCode: [PAGECODE.cardbody1, PAGECODE.cardbody2],
    };
    let data = cardSimpleData(simpleDataParams);
    //公式
    validateToSave(data, () => {
        ajax({
            method: 'post',
            url: URL.save,
            data: data,
            success: function (res) {
                const { data, formulamsg } = res;
                if (Array.isArray(formulamsg) && formulamsg.length) {
                    dealFormulamsg(res.formulamsg);
                }
                if (data) {
                    if (data.head) {
                        pageModel.get(PAGECODE.cardHead).setData({ [PAGECODE.cardHead]: data.head[PAGECODE.cardHead] });
                    }
                    if (data.bodys && data.bodys[PAGECODE.cardbody1]) {
                        pageModel.get(PAGECODE.cardbody1).updateRows(data.bodys[PAGECODE.cardbody1]);
                    }
                    if (data.bodys && data.bodys[PAGECODE.cardbody2]) {
                        pageModel.get(PAGECODE.cardbody2).updateRows(data.bodys[PAGECODE.cardbody2]);
                    }
                    let id = pageModel.get(PAGECODE.cardHead).getItemsValue(FIELD.pk).value;
                    //更新缓存
                    const dataSource = DataSource;
                    if (pageModel.pageTo.getUrlParam(FIELD.id)) {
                        updateCache(FIELD.pk, id, data, PAGECODE.cardHead, dataSource);//修改更新缓存
                    } else {
                        addCache(id, data, PAGECODE.cardHead, dataSource);//卡片页增加缓存
                        pageModel.get(PAGECODE.listTableId, PAGECODE.listCode).addCacheId(id);//列表页增加缓存
                        cardPagination.setCardPaginationId({ id: id })
                        pageModel.setUrlParam({ [FIELD.id]: id });
                    }
                    pageModel.updatePageStatus(STATUS.browse);
                    initButtonState(pageModel);
                    toast(multiLangData['4004POORDER-000005']); /* 国际化处理： 保存成功！*/
                }
            }
        });
    });
}
/*txBx9FIYfUQl2UukqRyP73+PxnwFNg9cG2D09QQ88Ef4PaPs4BTnj4ZfDFTpSXII*/